cmake_minimum_required(VERSION 3.10)

project(rknn_yolov8_tools)

# 设置生成 .so 和可执行文件的输出目录
set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin)

if (ENABLE_ASAN)
	message(STATUS "BUILD WITH ADDRESS SANITIZER")
	set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
	set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
	set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
endif ()

# 设置选项
set(TARGET_SOC "rk3588" CACHE STRING "Select the target SoC (options: rk356x, rk3588, rv110x)")
set(CMAKE_C_COMPILER "aarch64" CACHE STRING "Select the CMAKE_C_COMPILER (options: aarch64, armhf)")
set(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libraries")
# 打印 ENABLE_STATIC_LIB 和 BUILD_SHARED_LIBS 的值
message(STATUS "BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}")

if(NOT CMAKE_SYSTEM_PROCESSOR)
    if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
        set(CMAKE_SYSTEM_PROCESSOR "aarch64")
        message(WARNING "CMAKE_SYSTEM_PROCESSOR is not set. Defaulting to aarch64.")
    else()
        message(FATAL_ERROR "CMAKE_SYSTEM_PROCESSOR is not defined and cannot be inferred.")
    endif()
endif()

message(STATUS "CMAKE_SYSTEM_PROCESSOR = ${CMAKE_SYSTEM_PROCESSOR}")
# 确保定义后打印变量值
message(STATUS "CMAKE_SYSTEM_NAME = ${CMAKE_SYSTEM_NAME}")


set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# skip 3rd-party lib dependencies
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-shlib-undefined")

# install target and libraries
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/install/rknn_yolov8_tools_${CMAKE_SYSTEM_NAME})

set(CMAKE_SKIP_INSTALL_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

add_definitions(-DUSE_RKNN)

# rknn api
if(TARGET_SOC STREQUAL "rk356x")
  set(RKNN_API_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/RK356X/${CMAKE_SYSTEM_NAME}/librknn_api)
elseif(TARGET_SOC STREQUAL "rk3588")
  set(RKNN_API_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/rknpu2)
else()
  message(FATAL_ERROR "TARGET_SOC is not set, ref value: rk356x or rk3588 or rv110x")
endif()

if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
   set(LIB_ARCH aarch64)
else()
   set(LIB_ARCH armhf)
endif()

set(RKNN_RT_LIB ${RKNN_API_PATH}/Linux/${LIB_ARCH}/librknnrt.so)


include_directories(${RKNN_API_PATH}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty)

# opencv

if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
 set(OpenCV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/opencv/opencv-linux-aarch64/share/OpenCV)
else()
 set(OpenCV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/opencv/opencv-linux-armhf/share/OpenCV)
   
endif()

find_package(OpenCV REQUIRED)

#rga
if(TARGET_SOC STREQUAL "rk356x")
  set(RGA_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/rga/RK356X)
elseif(TARGET_SOC STREQUAL "rk3588")
  set(RGA_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/librga)
else()
  message(FATAL_ERROR "TARGET_SOC is not set, ref value: rk356x or rk3588")
endif()

if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  set(LIB_ARCH aarch64)
else()
  set(LIB_ARCH armhf)
endif()
  set(RGA_LIB ${RGA_PATH}/Linux//${LIB_ARCH}/librga.so)

include_directories( ${RGA_PATH}/include)

set(CMAKE_INSTALL_RPATH "lib")



#utils
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/utils)
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/stb_image)

#jpeg_turbo
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jpeg_turbo/include)
  set(JPEG_LIB ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jpeg_turbo/Linux/aarch64/libturbojpeg.a)


# rknn_yolov8_demo
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/include)


# 设置源码路径
file(GLOB SOURCE_FILES
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc"
   "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
   "${CMAKE_CURRENT_SOURCE_DIR}/utils/*.c"	
)



# 生成库和/或可执行文件
if(BUILD_SHARED_LIBS)
    add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
else()
  
    add_executable(${PROJECT_NAME} ${SOURCE_FILES})
endif()

target_link_libraries(${PROJECT_NAME}
  ${RKNN_RT_LIB}
  ${RGA_LIB}
  ${OpenCV_LIBS}
  ${JPEG_LIB}
)


# install target and libraries
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/install/rknn_yolov8_tools_${CMAKE_SYSTEM_NAME})
# 设置生成的库文件的安装路径
install(TARGETS ${PROJECT_NAME} 
    RUNTIME DESTINATION bin   # 可执行文件放到 BIN 目录
    LIBRARY DESTINATION lib   # 动态库放到 LIB 目录
    ARCHIVE DESTINATION lib)  # 静态库（如果有）放到 LIB 目录
	
	# 拷贝依赖库到 lib 文件夹
install(PROGRAMS ${RKNN_RT_LIB} DESTINATION ./lib)
install(PROGRAMS ${RGA_LIB} DESTINATION ./lib)
install(PROGRAMS ${JPEG_LIB} DESTINATION ./lib)
install(DIRECTORY model DESTINATION ./lib)
